﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;
using Dict.Blo;

namespace UI
{
    public class UIFieldRunMapTeamInfoSimpleHead : MonoBehaviour
    {
        public TextureKit heroIcon;
        public UISprite heroHp;
        public UILabel heroName;
        public UILabel heroScene;
        public bool online;
        [HideInInspector]
        public Material mat0;
        [HideInInspector]
        public Material mat1;
        
        public void Init(string heroIcon,string heroName,float hp,bool online)
        {
            this.online = online;

            TableHero heroData = DictHeroBlo.GetTableHero(heroIcon);

            if (heroData != null)
            {
                this.heroIcon.textureName = heroData.iconName;
            }
            else
            {
                Debug.LogError("[Error] heroId:[" + heroIcon+"]");
            }
           
            this.heroName.text = heroName;
            this.heroHp.fillAmount = hp;
            this.heroScene.text = online ? "":"["+ TableTextBlo.GetText("union_team_offline") + "]";
            if (heroData != null)
                SetOnLine();
        }

        void SetOnLine()
        {
            if (online)
            {
                if (mat0 == null)
                {
                    mat0 = Instantiate(heroIcon.texture.material) as Material;
                }
                heroIcon.texture.material = mat0;
                mat0.shader = Shader.Find("Unlit/Transparent Masked");
                heroHp.spriteName = "UI_yewai_xuetiao";

            }
            else
            {
                if (mat1 == null)
                {
                    mat1 = Instantiate(heroIcon.texture.material) as Material;
                }
                heroIcon.texture.material = mat1;
                mat1.shader = Shader.Find("Unlit/Masked DJMaskColorless");
                heroHp.spriteName = "UI_yewai_xuetiao_hui";
            }
        }
        
        void OnDestroy()
        {
            if (mat0 != null) Destroy(mat0);
            if (mat1 != null) Destroy(mat1);
        }
    }
}